package ru.hh.school.util;

import java.util.LinkedList;
import java.util.List;

public class BlockingQueue<T> {
    private final List<T> queue;
    private final int maxNumber;

    public BlockingQueue(int maxNumber) {
        this.queue = new LinkedList<T>();
        this.maxNumber = maxNumber;
    }

    public void add(T obj) {
        synchronized (queue) {
            if (queue.size() < this.maxNumber) {
                queue.add(obj);
            }
            queue.notify();
        }
    }

    public T take() throws InterruptedException {
        synchronized (queue) {
            while (queue.isEmpty()) {
                queue.wait();
            }
            return queue.remove(0);
        }
    }
}
